package com.chinaunicom.system.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.chinaunicom.system.dao.RoleMapper;
import com.chinaunicom.system.dto.RoleDto;
import com.chinaunicom.system.model.Role;
import com.chinaunicom.system.service.IRoleService;
@Service
public class RoleServiceImpl implements IRoleService {
	
	@Resource
	private RoleMapper dao;

	@Override
	public int deleteByPrimaryKey(Integer nRoleid) throws Exception {
		// TODO Auto-generated method stub
		return dao.deleteByPrimaryKey(nRoleid);
	}

	@Override
	public int insert(Role record) throws Exception {
		// TODO Auto-generated method stub
		return dao.insert(record);
	}

	@Override
	public int insertSelective(Role record) throws Exception {
		// TODO Auto-generated method stub
		return dao.insertSelective(record);
	}

	@Override
	public Role selectByPrimaryKey(Integer nRoleid) throws Exception {
		// TODO Auto-generated method stub
		return dao.selectByPrimaryKey(nRoleid);
	}

	@Override
	public int updateByPrimaryKeySelective(Role record) throws Exception {
		// TODO Auto-generated method stub
		return dao.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(Role record) throws Exception {
		// TODO Auto-generated method stub
		return dao.updateByPrimaryKey(record);
	}

	@Override
	public List<Role> query() throws Exception {
		// TODO Auto-generated method stub
		return dao.query();
	}

	@Override
	public List<Role> queryByName(String vRolename) {
		// TODO Auto-generated method stub
		return dao.queryByName(vRolename);
	}

	/**
	 * 保存角色数据
	 * 保存角色和菜单的关联数据
	 */
	@Override
	public void save(RoleDto dto) throws Exception {
		//保存角色数据
		Role role = dto.getRole();
		dao.insertSelective(role);
		//保存角色和菜单的关联关系
		List<Integer> mids = dto.getMids();
		if (mids !=null && mids.size()>0) {
			for (Integer mid : mids) {
				
				dao.saveRoleAndMenu(null,mid);
			}
		}
		
		
	}

	@Override
	public RoleDto queryById(Integer id) throws Exception {
		// TODO Auto-generated method stub
		return dao.queryById(id);
	}

	/**
	 * 更新角色信息
	 * 	1.根据id有选择的更新 
	 * 	2.更新和菜单的对应关系
	 * 		根据id删除原有关系
	 * 		然后插入新的关系
	 */
	@Override
	public void update(RoleDto dto) throws Exception {
		// TODO Auto-generated method stub
		//更新
		Role role = dto.getRole();
		dao.updateByPrimaryKeySelective(role);
		//根据id删除角色和菜单的原有关系
		dao.deleteMenuByRoleId(role.getnRoleid());
		List<Integer> mids = dto.getMids();
		if (mids != null && mids.size()>0) {
			for (Integer mid : mids) {
				//循环插入新的关系
				dao.saveRoleAndMenu(role.getnRoleid(), mid);
			}
			
		}
	}

	/**
	 * 删除角色信息 
	 * 先删除中间表
	 */
	@Override
	public void delete(Integer id) throws Exception {
		// TODO Auto-generated method stub
		dao.deleteUserByRoleId(id);
		dao.deleteMenuByRoleId(id);
		dao.deleteByPrimaryKey(id);
	}

}
